package com.yf.zzmusic.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yf.zzmusic.common.Constants;
import com.yf.zzmusic.common.PageInfo;
import com.yf.zzmusic.common.Result;
import com.yf.zzmusic.pojo.dto.sysDictData.SysDictDataPageDTO;
import com.yf.zzmusic.pojo.entity.Slide;
import com.yf.zzmusic.pojo.entity.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.yf.zzmusic.service.ISysDictDataService;
import com.yf.zzmusic.pojo.entity.SysDictData;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 字典表，根据type进行 前端控制器
 * </p>
 *
 * @author
 * @since 2024-02-28
 */
@RestController
@RequestMapping("/sysDictData")
public class SysDictDataController {
    @Resource
    private ISysDictDataService sysDictDataService;

    @PostMapping("/insert")
    public Result save(@Validated @RequestBody SysDictData sysDictData) {
        boolean result= sysDictDataService.save(sysDictData);
        if (result){
            return Result.success("新增成功",null);
        }else{
            return Result.error("新增失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean result = sysDictDataService.removeById(id);
        if (result){
            return Result.success("删除成功",null);
        }else{
            return Result.error(Constants.NO_CONTENT,"删除失败");
        }
    }

    @GetMapping
    public Result findAll() {
        List<SysDictData> sysDictDataList=sysDictDataService.list();
        if (sysDictDataList.size()>0){
            return Result.success("查询成功",sysDictDataList);
        }else {
            return Result.error(Constants.NO_CONTENT,"查询失败");
        }
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        SysDictData sysDictData=sysDictDataService.getById(id);
        if (sysDictData!=null){
            return Result.success("查询成功",sysDictData);
        }else{
            return Result.error(Constants.NO_CONTENT,"查询失败");
        }
    }

    @GetMapping("/page")
    public Result findByDictType(@Validated SysDictDataPageDTO sysDictDataPageDTO){
        PageInfo<SysDictData> pageInfo=sysDictDataService.sysDictDataByPage(sysDictDataPageDTO);
        if (pageInfo.getTotal()>0) {
            return Result.success("查询成功",pageInfo);
        }else{
            return Result.error(Constants.NO_CONTENT,"列表为空");
        }
    }
}

